import { js, JsonAsset } from "cc";
import { App } from "../core/app";
import { ResLoadHelper } from "../utils/resLoadHelper";
import { StorageHelper, StorageHelperKey } from "../utils/storageHelper";
import { Constant, LevelData, mapData, OriginLevelData } from "./enumConst";
import { gameConst } from "./gameConst";

class config {

  getCurLevel() {
    return gameConst.lv;
  }

  async getLevelData(id: number): Promise<LevelData> {

    id = gameConst.lv;
    let resData = await this.getGridData(id);
    let data: LevelData = {
      mapCount: resData.mapCount,
      blockCount: resData.blockCount,
      moveCount: resData.moveCount,
      scores: JSON.parse(resData.scores),
      blockRatio: JSON.parse(resData.blockRatio),
      mapData: [{
        m_id: JSON.parse(resData.m_id),
        m_ct: JSON.parse(resData.m_ct)
      }],
    }

    // 新增：解析关卡布局数组配置
    if (resData.layout) {
      try {
        data.layout = JSON.parse(resData.layout);
        console.log('关卡布局配置:', data.layout);
      } catch (e) {
        console.error('解析关卡布局配置失败:', e);
        data.layout = null;
      }
    }

    // 新增：解析障碍物层数配置
    if (resData.obstacleLayers) {
      try {
        data.obstacleLayers = JSON.parse(resData.obstacleLayers);
        console.log('障碍物层数配置:', data.obstacleLayers);
      } catch (e) {
        console.error('解析障碍物层数配置失败:', e);
        data.obstacleLayers = null;
      }
    }

    let list = [];
    for (let i = 0; i < data.mapData[0].m_id.length; i++) {
      let item = data.mapData[0].m_id[i];
      if (item > 5) {
        data.mapData[0].m_id[i] = this.handleIdArr(item);
      }
      let idx = list.indexOf(data.mapData[0].m_id[i]);
      if (idx < 0) {
        list.push(data.mapData[0].m_id[i])
      }
    };
    data.mapData[0].m_id = list;
    return data;
  }

  handleIdArr(id: number) {
    let numObj = {
      "201": 201,//3,
      "410": 4,
      "50": 0,
      "51": 1,
      "100": 2,
      "208": 4,
      "420": 0,
      "400": 1,
      "404": 2,
      "409": 3,
      "411": 0,
      "412": 1,
      "413": 2,
      "415": 3,
      "416": 4,
      "417": 0,
      "418": 1,
      "423": 2,
    }
    return numObj[`${id}`] || 0;
  }

  async getGridData(id: number = 1): Promise<OriginLevelData> {
    if (Number(id) > 1700) id = (+id % 1700) + 1;
    let json: JsonAsset = await ResLoadHelper.loadCommonAssetSync(`config/${id}`, JsonAsset);
    let loadData = json['json'] as OriginLevelData;
    return loadData;
  }

  setLevelStar(lv: number, num: number) {
    StorageHelper.setData(StorageHelperKey.Star + lv, num)
  }
  getLevelStar(lv: number) {
    return +StorageHelper.getData(StorageHelperKey.Star + lv)
  }

  getLevelTargetCount(list: mapData[], idx) {
    let ctArr = list[0].m_ct;
    let idArr = list[0].m_id;
    if (idArr[idx] > Constant.NormalType) {//是障碍物
      return ctArr[idx];
    }

    let count = ctArr[idx] + 10;
    if (ctArr[idx] < 10) {
      count = ctArr[idx] + 30;
    }
    return count;
  }
}

export let LevelConfig = new config();